package com.zhuliwei.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.TagListDto;
import com.zhuliwei.domain.entity.Tag;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.domain.vo.TagVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.service.TagService;
import com.zhuliwei.utils.BeanCopyUtils;
import com.zhuliwei.utils.WebUtils;
import io.swagger.annotations.Api;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签管理接口控制层
 *
 * @author 朱力维
 * @time 2022/10/25 14:59
 **/
@RestController
@RequestMapping("/content/tag")
@Api(tags="标签",description = "标签管理相关接口")
public class TagController {
    @Autowired
    private TagService tagService;

    @GetMapping("/list")
    @PreAuthorize("@ps.hasPermission('content:tag:list')")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        return tagService.pageTagList(pageNum, pageSize, tagListDto);
    }

    @PostMapping
    @PreAuthorize("@ps.hasPermission('content:tag:add')")
    public ResponseResult addTag(@RequestBody TagListDto tagListDto) {
        return tagService.addTag(tagListDto);
    }

    @DeleteMapping("/{ids}")
    @PreAuthorize("@ps.hasPermission('content:tag:delete')")
    public ResponseResult delTag(@PathVariable List<String> ids) {
        return tagService.delTag(ids);
    }

    @GetMapping("/{id}")
    public ResponseResult tagDetail(@PathVariable String id) {
        Tag tag = tagService.getById(id);
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @PutMapping
    @PreAuthorize("@ps.hasPermission('content:tag:update')")
    public ResponseResult updateTag(@RequestBody TagListDto tagListDto) {
        return tagService.updateTag(tagListDto);
    }
    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        List<TagVo> list = tagService.listAllTag();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/export")
    @PreAuthorize("@ps.hasPermission('content:tag:export')")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("标签.xlsx",response);
            //获取需要导出的数据
            List<Tag> tags = tagService.list();
            List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tags, TagVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), TagVo.class).autoCloseStream(Boolean.FALSE).sheet("标签导出")
                    .doWrite(tagVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }
}